package org.wu.framework.authorization.platform.infrastructure.persistence;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.wu.framework.authorization.platform.application.dto.SysPermissionDTO;
import org.wu.framework.authorization.platform.domain.model.sys.permission.SysPermission;
import org.wu.framework.authorization.platform.domain.model.sys.zone.SysZone;
import org.wu.framework.authorization.platform.infrastructure.converter.SysUserConverter;
import org.wu.framework.authorization.platform.infrastructure.entity.*;
import org.wu.framework.authorization.platform.domain.model.sys.menu.SysMenu;
import org.wu.framework.authorization.platform.domain.model.sys.role.SysRole;
import org.wu.framework.authorization.platform.domain.model.sys.user.SysUser;
import org.wu.framework.authorization.platform.domain.model.sys.user.SysUserRepository;
import org.wu.framework.authorization.platform.domain.model.sys.user.role.SysUserRole;
import org.wu.framework.inner.lazy.orm.spring.annotation.LazyIgnoreSqlInterceptor;
import org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyInfrastructurePersistence;
import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;
import org.wu.framework.lazy.orm.database.lambda.stream.lambda.LazyLambdaStream;
import org.wu.framework.lazy.orm.database.lambda.stream.wrapper.LazyWrappers;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;

import java.util.List;

/**
 * describe
 *
 * @author Jia wei Wu
 * @date 2023/05/17 11:19 晚上
 * @see DefaultDDDLazyInfrastructurePersistence
 **/
@Repository
public class SysUserRepositoryImpl implements SysUserRepository {

    @Autowired
    LazyLambdaStream lazyLambdaStream;


    /**
     * describe 新增
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<SysUser> story(SysUser sysUser) {
        SysUserDO sysUserDO = SysUserConverter.fromSysUser(sysUser);
        lazyLambdaStream.upsert(sysUserDO);
        return ResultFactory.successOf(SysUserConverter.toSysUser(sysUserDO));
    }

    /**
     * describe 查询单个
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<SysUser> findOne(SysUser sysUser) {
        SysUserDO sysUserDO = SysUserConverter.fromSysUser(sysUser);
        SysUser sysUserOne = lazyLambdaStream.selectOne(LazyWrappers.lambdaWrapperBean(sysUserDO), SysUser.class);
        return ResultFactory.successOf(sysUserOne);
    }

    /**
     * describe 查询多个
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<List<SysUser>> findList(SysUser sysUser) {
        SysUserDO sysUserDO = SysUserConverter.fromSysUser(sysUser);
        List<SysUser> sysUserList = lazyLambdaStream.selectList(LazyWrappers.lambdaWrapperBean(sysUserDO), SysUser.class);
        sysUserList.stream().forEach(one -> {
            // 获取用户角色
            List<SysRole> sysRoleList = lazyLambdaStream.selectList(LazyWrappers.<SysUserRole>lambdaWrapper()
                    .eq(SysUserRole::getIsDeleted, false)
                    .eq(SysUserRole::getUserId, one.getId())
                    .internalJoin(LazyWrappers.<SysUserRole, SysRoleDO>lambdaWrapperJoin()
                            .eqo(SysUserRole::getUserId, one.getId())
                            .eqo(SysUserRole::getIsDeleted, false)
                            .eq(SysUserRole::getRoleId, SysRoleDO::getId)
                            .eqRighto(SysRoleDO::getIsDeleted, false)
                    )
                    .onlyUseAs()
                    .as(SysRoleDO.class), SysRole.class
            );

            // 获取角色菜单
            sysRoleList.stream().forEach(role -> {
                List<SysMenu> sysMenuList = lazyLambdaStream.selectList(LazyWrappers.<SysRoleMenuDO>lambdaWrapper()
                                .eq(SysRoleMenuDO::getIsDeleted, false)
                                .eq(SysRoleMenuDO::getRoleId, role.getId())
                                .internalJoin(LazyWrappers.<SysRoleMenuDO, SysMenuDO>lambdaWrapperJoin()
                                        .eqo(SysRoleMenuDO::getIsDeleted, false)
                                        .eqo(SysRoleMenuDO::getRoleId, role.getId())
                                        .eq(SysRoleMenuDO::getMenuId, SysMenuDO::getId)
                                        .eqRighto(SysMenuDO::getIsDeleted, false)
                                )
                                .onlyUseAs()
                                .as(SysMenu.class),
                        SysMenu.class
                );
                role.setSysMenuList(sysMenuList);
            });
            one.setSysRoleList(sysRoleList);
        });
        return ResultFactory.successOf(sysUserList);
    }

    /**
     * describe 分页查询多个
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/21 11:16 晚上
     **/

    @Override
    public Result<LazyPage<SysUser>> findPage(int size, int current, SysUser sysUser) {
        SysUserDO sysUserDO = SysUserConverter.fromSysUser(sysUser);
        LazyPage lazyPage = new LazyPage(current, size);
        LazyPage<SysUser> sysUserLazyPage = lazyLambdaStream.selectPage(LazyWrappers.lambdaWrapperBean(sysUserDO), lazyPage, SysUser.class);
        sysUserLazyPage.getRecords().stream().forEach(one -> {
            List<SysRole> sysRoleList = lazyLambdaStream.selectList(LazyWrappers.<SysUserRole>lambdaWrapper()
                    .eq(SysUserRole::getIsDeleted, false)
                    .eq(SysUserRole::getUserId, one.getId())
                    .internalJoin(LazyWrappers.<SysUserRole, SysRoleDO>lambdaWrapperJoin()
                            .eqo(SysUserRole::getUserId, one.getId())
                            .eqo(SysUserRole::getIsDeleted, false)
                            .eq(SysUserRole::getRoleId, SysRoleDO::getId)
                            .eqRighto(SysRoleDO::getIsDeleted, false)
                    )
                    .onlyUseAs()
                    .as(SysRoleDO.class), SysRole.class
            );
            one.setSysRoleList(sysRoleList);
        });
        return ResultFactory.successOf(sysUserLazyPage);
    }

    /**
     * describe 删除
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<SysUser> delete(SysUser sysUser) {
        SysUserDO sysUserDO = SysUserConverter.fromSysUser(sysUser);
        //  lazyLambdaStream.remove(sysUserDO);
        return ResultFactory.successOf();
    }

    /**
     * 获取用户区域
     *
     * @param userId 用户ID
     * @return
     */
    @LazyIgnoreSqlInterceptor
    @Override
    public Result<List<SysZone>> findUseZoneList(Long userId) {
        // 查询用户对应的区域
        List<SysZone> sysZoneList = lazyLambdaStream.selectList(
                LazyWrappers.<SysZoneUserDO>lambdaWrapper()
                        .eq(SysZoneUserDO::getUserId, userId)
                        .eq(SysZoneUserDO::getIsDeleted, false)
                        .internalJoin(LazyWrappers.<SysZoneUserDO, SysZoneDO>lambdaWrapperJoin()
                                .eq(SysZoneUserDO::getZoneCode, SysZoneDO::getCode)
                                .eqo(SysZoneUserDO::getIsDeleted, false)
                                .eqRighto(SysZoneDO::getIsDeleted, false)
                        )
                        .onlyUseAs()
                        .as(SysZoneDO.class),
                SysZone.class
        );
        // 查询区域信息
        return ResultFactory.successOf(sysZoneList);
    }

    /**
     * 获取用户权限
     *
     * @param userId 用户ID
     * @return
     */
    @LazyIgnoreSqlInterceptor
    @Override
    public Result<List<SysPermission>> findUserPermissionList(Long userId) {
        List<SysPermission> sysPermissionList =
                lazyLambdaStream.selectList(
                        LazyWrappers.<SysUserRoleDO>lambdaWrapper()
                                .eq(SysUserRoleDO::getUserId, userId)
                                .eq(SysUserRoleDO::getIsDeleted, false)
                                .internalJoin(
                                        LazyWrappers.<SysUserRoleDO, SysRolePermissionDO>lambdaWrapperJoin()
                                                .eq(SysUserRoleDO::getUserId, SysRolePermissionDO::getRoleId)
                                                .eqo(SysUserRoleDO::getIsDeleted, false)
                                                .eqRighto(SysRolePermissionDO::getIsDeleted, false)
                                )
                                .internalJoin(
                                        LazyWrappers.<SysRolePermissionDO,SysPermissionDO>lambdaWrapperJoin()
                                                .eq(SysRolePermissionDO::getPermissionId,SysPermissionDO::getId)
                                                .eqo(SysRolePermissionDO::getIsDeleted, false)
                                                .eqRighto(SysPermissionDO::getIsDeleted, false)

                                )
                                .onlyUseAs()
                                .as(SysPermissionDO.class),
                        SysPermission.class
                );
        return ResultFactory.successOf(sysPermissionList);
    }
}